import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutterhello/api/api.dart';
import 'package:flutterhello/controller/Controller.dart';
import 'package:flutterhello/pages/notice/widget/act_item.dart';
import 'package:flutterhello/pages/notice/widget/notice_item.dart';
import 'package:flutterhello/widgets/my_toast.dart';
import 'package:get/get.dart';

class NoticePage extends StatefulWidget {
  @override
  _NoticePageState createState() => _NoticePageState();
}

class _NoticePageState extends State<NoticePage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<Controller>(builder: (_controller) {
      return DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: PreferredSize(
            child: AppBar(
              title: Text('系统消息'),
              centerTitle: true,
              bottom: TabBar(
                // isScrollable: true, //可滚动
                indicatorColor: Colors.blueGrey, //指示器的颜色
                labelColor: Colors.white, //选中文字颜色
                unselectedLabelColor: Colors.black54, //为选中文字颜色
                // indicatorSize: TabBarIndicatorSize.label, //指示器与文字等宽
                tabs: <Widget>[
                  Tab(text: "招募"),
                  Tab(text: "活动"),
                ],
              ),
            ),
            preferredSize: Size.fromHeight(90),
          ),
          body: TabBarView(
            children: [
              EasyRefresh.custom(
                header: MaterialHeader(),
                footer: MaterialFooter(),
                onRefresh: () async {
                  await _controller.getRecruitNews();
                },
                slivers: <Widget>[
                  SliverToBoxAdapter(
                    child: SizedBox(
                      height: 20,
                    ),
                  ),
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                      (BuildContext context, int index) {
                        return GestureDetector(
                          child: NoticeItem(
                            info: _controller.recruit_news[index],
                          ),
                          onLongPress: () {
                            showDialog(
                              context: context,
                              builder: (context) {
                                return AlertDialog(
                                  title: Text('提示'),
                                  content: Text('确认删除这条消息吗？'),
                                  actions: <Widget>[
                                    TextButton(
                                      child: Text('取消'),
                                      onPressed: () {
                                        Navigator.of(context).pop('cancel');
                                      },
                                    ),
                                    TextButton(
                                      child: Text('确认'),
                                      onPressed: () async {
                                        deleteRecruitNews(
                                          context,
                                          _controller.recruit_news[index]['id'],
                                          index,
                                        );
                                      },
                                    ),
                                  ],
                                );
                              },
                            );
                          },
                        );
                      },
                      childCount: _controller.recruit_news.length,
                    ),
                  )
                ],
              ),
              EasyRefresh.custom(
                header: MaterialHeader(),
                footer: MaterialFooter(),
                onRefresh: () async {
                  await _controller.getActiviteNews();
                },
                slivers: <Widget>[
                  SliverToBoxAdapter(
                    child: SizedBox(
                      height: 20,
                    ),
                  ),
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                      (BuildContext context, int index) {
                        return GestureDetector(
                          child: ActItem(
                            info: _controller.activite_news[
                                _controller.activite_news.length - index - 1],
                          ),
                          onLongPress: () {
                            showDialog(
                              context: context,
                              builder: (context) {
                                return AlertDialog(
                                  title: Text('提示'),
                                  content: Text('确认删除这条消息吗？'),
                                  actions: <Widget>[
                                    TextButton(
                                      child: Text('取消'),
                                      onPressed: () {
                                        Navigator.of(context).pop('cancel');
                                      },
                                    ),
                                    TextButton(
                                      child: Text('确认'),
                                      onPressed: () async {
                                        deleteActiviteNews(
                                          context,
                                          _controller.activite_news[index]
                                              ['id'],
                                          index,
                                        );
                                      },
                                    ),
                                  ],
                                );
                              },
                            );
                          },
                        );
                      },
                      childCount: _controller.activite_news.length,
                    ),
                  )
                ],
              )
            ],
          ),
        ),
      );
    });
  }
}

deleteRecruitNews(context, id, index) async {
  var res = await Api.deleteRecruitNews({
    "newsID": id,
  });
  if (res['code'] == 1000) {
    final controller = Get.put(Controller());
    controller.recruit_news.removeAt(index);
    controller.myUpdata();
  }
  MyTost.showTost(res['message']);
  Navigator.of(context).pop('cancel');
}

deleteActiviteNews(context, id, index) async {
  var res = await Api.deleteActiviteNews({
    "newsID": id,
  });
  if (res['code'] == 1000) {
    final controller = Get.put(Controller());
    controller.activite_news.removeAt(index);
    controller.myUpdata();
  }
  MyTost.showTost(res['message']);
  Navigator.of(context).pop('cancel');
}
